package com.doctor.windflower_doctor.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.QuestionAction;
import com.doctor.windflower_doctor.adapter.HistoryAskingAdapter;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.entity.QuestionBeen;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.view.LoadingDialog;
import com.doctor.windflower_doctor.view.PullToCustomListView;
import com.doctor.windflower_doctor.view.PullToCustomListView.OnAutoLoadBottomListener;
import com.doctor.windflower_doctor.view.PullToCustomListView.OnRefreshDownListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;

/**
 * <p>
 * 功能 提问记录
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年11月6日 下午2:56:47
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class HistoryAskingActivity extends BaseActivity implements Constent, OnClickListener {
	private ListView questionList;
	private PullToCustomListView customListView;
	private String userId;
	private LoadingDialog loadingDialog;
	private HistoryAskingAdapter historyAskingAdapter;
	private int type = 1;
	private String id = "";
	private TextView textView;
	private ImageButton back_btn;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {

	}

	@Override
	protected void processLogic() {

	}

	@Override
	protected void setListener() {
		historyAskingAdapter = new HistoryAskingAdapter(HistoryAskingActivity.this);
		questionList.setAdapter(historyAskingAdapter);
		customListView.setOnRefreshDownListener(new OnRefreshDownListener() {

			@Override
			public void onRefresh() {
				type = 1;
				id = "";
				getList();
			}
		});
		customListView.setOnAutoLoadBottomListener(new OnAutoLoadBottomListener() {

			@Override
			public void onBottomLoad() {
				type = 0;
				getList();
			}
		});
		back_btn.setOnClickListener(this);
	}

	@Override
	protected void findViews() {
		loadingDialog = new LoadingDialog(this);
		Bundle bundle = getIntent().getExtras();
		userId = bundle.getString(USERID);
		textView = (TextView) findViewById(R.id.textView);
		textView.setText("提问记录");
		back_btn = (ImageButton) findViewById(R.id.back_btn);
		customListView = (PullToCustomListView) findViewById(R.id.custom_list);
		questionList = customListView.getRefreshableView();
	}

	@Override
	protected void onStart() {
		getList();
		super.onStart();
	}

	@Override
	public int getResLayoutId() {

		return R.layout.activity_history_asking;
	}

	/**
	 * <p>
	 * 功能 获取提问历史
	 * </p>
	 * 
	 * @author 皮勉 时间 2014年11月7日 下午3:57:22
	 */
	void getList() {
		HttpMethod requestMethod = HttpMethod.GET;
		String requestUrl = URL + ASK_DOCTOR_USERQUESTIONS + "?" + DOCTORID + "=" + SharedPreferencesDB.getInstance(this).getDocotorId() + "&" + USERID + "="
				+ userId + "&" + TYPE + "=" + type + "&size=10&" + ID + "=" + id;
		LogUtils.e("requestUrl>>>>>>>>>" + requestUrl);
		HttpRequestCallBack<QuestionAction> requestCallBack = new HttpRequestCallBack<QuestionAction>(new JsonParser(), QuestionAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<QuestionAction> responseInfo) {
				loadingDialog.close();
				if (responseInfo.success.success) {
					if (type == 1) {
						customListView.onRefreshDownComplete();
					} else {
						customListView.onLoadBottomComplete();
					}
					QuestionBeen questionBeen = responseInfo.success.data;
					if (questionBeen != null) {
						LogUtils.e("==============================" + questionBeen.questions.size());
						if (questionBeen.questions != null && questionBeen.questions.size() > 0) {
							if (type == 1 && historyAskingAdapter.getCount() > 0)
								historyAskingAdapter.clearData();
							historyAskingAdapter.addListData(questionBeen.questions);
							id = questionBeen.questions.get(questionBeen.questions.size() - 1).id;
						}
					}
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				loadingDialog.close();
				LogUtils.e("=====getUserInfermation====3===" + msg);
			}

			@Override
			public void onStart() {
				loadingDialog.show();
				super.onStart();

			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, requestUrl, requestCallBack);
		HttpRequest.send(params);
	}

	@Override
	public void onClick(View arg0) {
		finish();
	}
}
